package com.fili.core.svn.components;

import com.fili.core.svn.SvnEntry;

import javax.swing.*;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Oct 19, 2006
 * Time: 5:24:44 PM
 * Revision: 0
 */
public class SvnTree extends JComponent implements TreeWillExpandListener, MouseListener
{
    private SvnTreeListener listener;
    public JTree tree;
    private CheckNode root;
    public DefaultTreeModel treeModel;
    public static ImageIcon icoFolderClosed = null;
    public static ImageIcon icoFolderOpened = null;
    public static ImageIcon icoFolderFull = null;
    public static Image icoOverlayGreen = null;
    public static Image icoOverlayBlue = null;
    public static Image icoOverlayRed = null;
    public static Image icoOverlayWait = null;


    public SvnTree(SvnTreeListener listener)
    {
        this.listener = listener;

        if (icoFolderClosed == null)
            icoFolderClosed = new ImageIcon(getToolkit().getImage("res/img/folder_closed.png"));
        if (icoFolderOpened == null)
            icoFolderOpened = new ImageIcon(getToolkit().getImage("res/img/folder_opened.png"));
        if (icoFolderFull == null)
            icoFolderFull = new ImageIcon(getToolkit().getImage("res/img/folder_full.png"));
        if (icoOverlayBlue == null)
            icoOverlayBlue = getToolkit().getImage("res/img/overlay_blue.png");
        if (icoOverlayGreen == null)
            icoOverlayGreen = getToolkit().getImage("res/img/overlay_green.png");
        if (icoOverlayRed == null)
            icoOverlayRed = getToolkit().getImage("res/img/overlay_red.png");
        if (icoOverlayWait == null)
            icoOverlayWait = getToolkit().getImage("res/img/overlay_wait.png");

        root = new CheckNode(null);

        treeModel = new DefaultTreeModel(root);
        treeModel.insertNodeInto(new CheckNode("PLEASE_WAIT", CheckNode.CHECK_WAIT), root, 0);

        tree = new JTree(treeModel);
        tree.setCellRenderer(new SvnTreeRenderer());
        tree.setShowsRootHandles(true);
        tree.addTreeWillExpandListener(this);
        tree.addMouseListener(this);
//        tree.setRootVisible(false);
        setLayout(new BorderLayout());
        add(new JScrollPane(tree), BorderLayout.CENTER);

        listener.populateNode(root);
    }

    public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException
    {
        CheckNode node = (CheckNode) event.getPath().getLastPathComponent();
        while (node.getChildCount() > 1)
            treeModel.removeNodeFromParent((MutableTreeNode) node.getChildAt(0));
        if (node.getChildCount() == 1)
        {
            CheckNode n = (CheckNode) node.getChildAt(0);
            n.setType(CheckNode.CHECK_WAIT);
            n.setUserObject("PLEASE WAIT");
        }
    }

    public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException
    {
        listener.populateNode((CheckNode) event.getPath().getLastPathComponent());
    }

    public void mouseClicked(MouseEvent e)
    {
        TreePath path = tree.getPathForLocation(e.getX(), e.getY());
        if (path == null)
            return;
        CheckNode node = (CheckNode) path.getLastPathComponent();
        if (node == null)
            return;
        Rectangle r = tree.getPathBounds(path);
        if (e.getX() < r.x + 16)
        {
            if (node.getType() != CheckNode.CHECK_NONE)
                node.setType(CheckNode.CHECK_GREEN);
            else
                node.setType(CheckNode.CHECK_NONE);
            treeModel.nodeChanged(node);
        }
    }

    public void mouseEntered(MouseEvent e)
    {
    }

    public void mouseExited(MouseEvent e)
    {
    }

    public void mousePressed(MouseEvent e)
    {
    }

    public void mouseReleased(MouseEvent e)
    {
    }
}
